import * as fs from 'fs';
import * as path from 'path';
export class BaseCreater {

    constructor(template?: Template | any) {
        if (template !== undefined) {
            if (template instanceof Template) this.template = template;
            else this.template = Template.fromJs(template);
        }
    }

    template: Template = Template.fromJs({});

    createFile(name: string, content: string = ''): Promise<any> {
        return new Promise<any>((resolve, reject) => {
            fs.open(name, 'w', function (e, fd) {
                if (e) reject(e);

                fs.write(fd, content, 0, 'utf-8', function (e) {
                    if (e) reject(e);
                    fs.closeSync(fd);
                    resolve(true);
                });
            });
        });

    }

}

export class Template {
    ts: string;
    html: string;
    less: string;
    constructor(js?: any) {
        this._init(js);
    }
    private _init(js?: any) {
        if (js !== undefined) {
            this.ts = js['ts'] !== undefined ? js['ts'] : '';
            this.html = js['html'] !== undefined ? js['html'] : '';
            this.less = js['less'] !== undefined ? js['less'] : '';
        }
    }
    static fromJs(js: any) {
        return new Template(js);
    }

}